package com.lx.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lx.entity.LxEnv;
import com.lx.entity.LxEqu;
import com.lx.entity.LxUser;
import com.lx.service.impl.LxEnvServiceImpl;
import com.lx.service.impl.LxEquServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵敬轩
 * @since 2021-09-12
 */
@RestController
@RequestMapping("/lx-equ")
public class LxEquController {
    @Autowired
    private LxEquServiceImpl lxEquService;
    @CrossOrigin
    @GetMapping("/getAll")
    public String getAllEqu(Integer page, Integer limit){
        IPage<LxEqu> lxEquIPage = lxEquService.selectAllEqu(page, limit);
        List<LxEqu> list = lxEquService.list();
        Map<String,Object> resultData=new HashMap<>();
        resultData.put("code",0);
        resultData.put("msg",null);
        resultData.put("data",lxEquIPage.getRecords());
        resultData.put("count",list.size());
        return JSON.toJSONString(resultData);
    }

    @CrossOrigin
    @GetMapping("/getAllEquName")
    public String getAllEquName(){
        List<LxEqu> list = lxEquService.list();
        Map<String,Object> resultData = new HashMap<>();
        resultData.put("code",0);
        resultData.put("msg","查询全部设备");
        resultData.put("data",list);
        resultData.put("count",list.size());
        System.out.println(list);
        return JSON.toJSONString(resultData);
    }

    @CrossOrigin
    @GetMapping("/delete")
    public String deleteEqu(String id){
        HashMap<String , Object> resultData = new HashMap<>();
        System.out.println(id);

        boolean deleteEquId = lxEquService.removeById(id);
        if (deleteEquId)
            resultData.put("msg","删除成功");
        else
            resultData.put("msg","删除失败");
        return JSON.toJSONString(resultData);
    }


    @CrossOrigin
    @PostMapping("/save")
    public String updateEqu(@RequestBody LxEqu lxEqu){
        System.out.println(lxEqu);
        HashMap<String, Object> resultDate = new HashMap<>();
        boolean updateEqu = lxEquService.saveOrUpdate(lxEqu);
        if (updateEqu)
            resultDate.put("msg","信息更新成功");
        else
            resultDate.put("msg","信息更新失败");
        return JSON.toJSONString(resultDate);
    }

    @CrossOrigin
    @GetMapping("/search")
    public String search(String equid, String ename, Integer page, Integer limit) {
        System.out.println(page + " " + limit + " " + equid + " " + ename);

        if (equid.isEmpty() && ename.isEmpty()) {
            IPage<LxEqu> lxEquIPage = lxEquService.selectAllEqu(page, limit);
            List<LxEqu> list = lxEquService.list();
            Map<String, Object> resultData = new HashMap<>();
            resultData.put("code", 0);
            resultData.put("msg", null);
            resultData.put("data", lxEquIPage.getRecords());
            resultData.put("count", list.size());
            return JSON.toJSONString(resultData);
        } else {
            if (equid.isEmpty()) {
                IPage<LxEqu> lxUserIPage = lxEquService.selectAllEqu(page, limit);
                Map<String, Object> columnMap = new HashMap<String, Object>();
                List list = lxEquService.selectByName(ename);
                List<LxEqu> userList = new ArrayList<>();
                Map<String, Object> resultData = new HashMap<>();
                resultData.put("code", 0);
                resultData.put("msg", null);
                resultData.put("data", list);
                resultData.put("count", list.size());
                return JSON.toJSONString(resultData);
            } else {
                LxEqu user = lxEquService.getById(equid);
                List<LxEqu> list = new ArrayList<>();
                list.add(user);
                System.out.println("controller的" + list);
                Map<String, Object> resultData = new HashMap<>();
                resultData.put("code", 0);
                resultData.put("msg", null);
                resultData.put("data", list);
                resultData.put("count", 1);
                return JSON.toJSONString(resultData);
            }

        }
    }


}
